{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6cce5e50",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import QuantumCircuit, qasm3\n",
    "from quantum_chip import QuantumChip\n",
    "from hardware import HardwareParams\n",
    "from qrmove_compiler import QRMoveCompiler\n",
    "from utils import get_quantum_circuit\n",
    "import math\n",
    "from func_timeout import func_timeout, FunctionTimedOut\n",
    "import csv\n",
    "\n",
    "\n",
    "def experiment(quantum_alg, qubit_num, extra_qubit_num, MRP_CP_ratio):\n",
    "    \"\"\"\n",
    "    quantum_alg: 量子算法\n",
    "    qubit_num: 量子比特数量\n",
    "    extra_qubit_ratio: 额外比特比例\n",
    "    MRP_CP_ratio: MRP/CP 比例\n",
    "    \"\"\"\n",
    "    try:\n",
    "        quantum_circuit: QuantumCircuit = get_quantum_circuit(quantum_alg, qubit_num)\n",
    "        quantum_chip = QuantumChip(\"square\", 50)\n",
    "        hardware_param = HardwareParams(\n",
    "            time_1q=300.0,  # 单比特门时间\n",
    "            time_2q=300.0,  # 双比特门时间\n",
    "            time_meas=300 * MRP_CP_ratio,  # 测量时间\n",
    "            time_reset=0,  # 重置时间\n",
    "        )\n",
    "        qmc = QRMoveCompiler(\n",
    "            quantum_circuit,\n",
    "            quantum_chip,\n",
    "            hardware_param,\n",
    "            math.ceil(extra_qubit_num),\n",
    "        )\n",
    "        (\n",
    "            start_depth,\n",
    "            start_width,\n",
    "            qr_map_depth,\n",
    "            qr_map_width,\n",
    "            qr_move_depth,\n",
    "            qr_move_width,\n",
    "        ) = qmc.compile_program()\n",
    "\n",
    "        return (\n",
    "            start_depth,\n",
    "            start_width,\n",
    "            qr_map_depth,\n",
    "            qr_map_width,\n",
    "            qr_move_depth,\n",
    "            qr_move_width,\n",
    "        )\n",
    "\n",
    "    except Exception as e:\n",
    "        print(f\"Experiment failed with error: {e}\")\n",
    "        return 0, 0, 0, 0, 0, 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "586cdb83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alg: bv, qubit_num: 10\n",
      "运行结果: start_depth 14, start_width 10, qr_map_depth 41, qr_map_width 2, qr_move_depth 22, qr_move_width 3\n",
      "运行结果: start_depth 14, start_width 10, qr_map_depth 41, qr_map_width 2, qr_move_depth 23, qr_move_width 3\n",
      "运行结果: start_depth 14, start_width 10, qr_map_depth 41, qr_map_width 2, qr_move_depth 22, qr_move_width 3\n",
      "运行结果: start_depth 14, start_width 10, qr_map_depth 41, qr_map_width 2, qr_move_depth 23, qr_move_width 3\n",
      "运行结果: start_depth 14, start_width 10, qr_map_depth 41, qr_map_width 2, qr_move_depth 22, qr_move_width 3\n",
      "alg: bv, qubit_num: 20\n",
      "运行结果: start_depth 19, start_width 20, qr_map_depth 91, qr_map_width 2, qr_move_depth 44, qr_move_width 4\n",
      "运行结果: start_depth 19, start_width 20, qr_map_depth 91, qr_map_width 2, qr_move_depth 35, qr_move_width 4\n",
      "运行结果: start_depth 19, start_width 20, qr_map_depth 91, qr_map_width 2, qr_move_depth 34, qr_move_width 4\n",
      "运行结果: start_depth 19, start_width 20, qr_map_depth 91, qr_map_width 2, qr_move_depth 43, qr_move_width 4\n",
      "运行结果: start_depth 19, start_width 20, qr_map_depth 91, qr_map_width 2, qr_move_depth 43, qr_move_width 4\n",
      "alg: bv, qubit_num: 30\n",
      "运行结果: start_depth 24, start_width 30, qr_map_depth 141, qr_map_width 2, qr_move_depth 55, qr_move_width 4\n",
      "运行结果: start_depth 24, start_width 30, qr_map_depth 141, qr_map_width 2, qr_move_depth 73, qr_move_width 3\n",
      "运行结果: start_depth 24, start_width 30, qr_map_depth 141, qr_map_width 2, qr_move_depth 64, qr_move_width 4\n",
      "运行结果: start_depth 24, start_width 30, qr_map_depth 141, qr_map_width 2, qr_move_depth 56, qr_move_width 4\n",
      "运行结果: start_depth 24, start_width 30, qr_map_depth 141, qr_map_width 2, qr_move_depth 63, qr_move_width 4\n",
      "alg: bv, qubit_num: 40\n",
      "运行结果: start_depth 29, start_width 40, qr_map_depth 191, qr_map_width 2, qr_move_depth 84, qr_move_width 4\n",
      "运行结果: start_depth 29, start_width 40, qr_map_depth 191, qr_map_width 2, qr_move_depth 75, qr_move_width 4\n",
      "运行结果: start_depth 29, start_width 40, qr_map_depth 191, qr_map_width 2, qr_move_depth 73, qr_move_width 4\n",
      "运行结果: start_depth 29, start_width 40, qr_map_depth 191, qr_map_width 2, qr_move_depth 86, qr_move_width 4\n",
      "运行结果: start_depth 29, start_width 40, qr_map_depth 191, qr_map_width 2, qr_move_depth 75, qr_move_width 4\n",
      "alg: bv, qubit_num: 50\n",
      "运行结果: start_depth 34, start_width 50, qr_map_depth 232, qr_map_width 3, qr_move_depth 105, qr_move_width 5\n",
      "运行结果: start_depth 34, start_width 50, qr_map_depth 232, qr_map_width 3, qr_move_depth 99, qr_move_width 5\n",
      "运行结果: start_depth 34, start_width 50, qr_map_depth 232, qr_map_width 3, qr_move_depth 96, qr_move_width 5\n",
      "运行结果: start_depth 34, start_width 50, qr_map_depth 232, qr_map_width 3, qr_move_depth 107, qr_move_width 5\n",
      "运行结果: start_depth 34, start_width 50, qr_map_depth 232, qr_map_width 3, qr_move_depth 99, qr_move_width 5\n",
      "alg: qaoa, qubit_num: 10\n",
      "运行结果: start_depth 52, start_width 10, qr_map_depth 88, qr_map_width 7, qr_move_depth 79, qr_move_width 7\n",
      "运行结果: start_depth 52, start_width 10, qr_map_depth 88, qr_map_width 7, qr_move_depth 79, qr_move_width 7\n",
      "运行结果: start_depth 52, start_width 10, qr_map_depth 88, qr_map_width 7, qr_move_depth 79, qr_move_width 7\n",
      "运行结果: start_depth 52, start_width 10, qr_map_depth 88, qr_map_width 7, qr_move_depth 79, qr_move_width 7\n",
      "运行结果: start_depth 52, start_width 10, qr_map_depth 88, qr_map_width 7, qr_move_depth 79, qr_move_width 7\n",
      "alg: qaoa, qubit_num: 20\n",
      "运行结果: start_depth 97, start_width 20, qr_map_depth 196, qr_map_width 16, qr_move_depth 130, qr_move_width 18\n",
      "运行结果: start_depth 97, start_width 20, qr_map_depth 196, qr_map_width 16, qr_move_depth 142, qr_move_width 18\n",
      "运行结果: start_depth 97, start_width 20, qr_map_depth 196, qr_map_width 16, qr_move_depth 142, qr_move_width 18\n",
      "运行结果: start_depth 97, start_width 20, qr_map_depth 196, qr_map_width 16, qr_move_depth 133, qr_move_width 17\n",
      "运行结果: start_depth 97, start_width 20, qr_map_depth 196, qr_map_width 16, qr_move_depth 118, qr_move_width 17\n",
      "alg: qaoa, qubit_num: 30\n",
      "运行结果: start_depth 145, start_width 30, qr_map_depth 202, qr_map_width 27, qr_move_depth 145, qr_move_width 29\n",
      "运行结果: start_depth 145, start_width 30, qr_map_depth 202, qr_map_width 27, qr_move_depth 145, qr_move_width 29\n",
      "运行结果: start_depth 145, start_width 30, qr_map_depth 202, qr_map_width 27, qr_move_depth 145, qr_move_width 29\n",
      "运行结果: start_depth 145, start_width 30, qr_map_depth 202, qr_map_width 27, qr_move_depth 151, qr_move_width 29\n",
      "运行结果: start_depth 145, start_width 30, qr_map_depth 202, qr_map_width 27, qr_move_depth 151, qr_move_width 29\n",
      "alg: qaoa, qubit_num: 40\n",
      "运行结果: start_depth 190, start_width 40, qr_map_depth 346, qr_map_width 37, qr_move_depth 190, qr_move_width 38\n",
      "运行结果: start_depth 190, start_width 40, qr_map_depth 346, qr_map_width 37, qr_move_depth 256, qr_move_width 37\n",
      "运行结果: start_depth 190, start_width 40, qr_map_depth 346, qr_map_width 37, qr_move_depth 190, qr_move_width 39\n",
      "运行结果: start_depth 190, start_width 40, qr_map_depth 346, qr_map_width 37, qr_move_depth 295, qr_move_width 37\n",
      "运行结果: start_depth 190, start_width 40, qr_map_depth 346, qr_map_width 37, qr_move_depth 190, qr_move_width 39\n",
      "alg: qaoa, qubit_num: 50\n",
      "运行结果: start_depth 250, start_width 50, qr_map_depth 421, qr_map_width 46, qr_move_depth 358, qr_move_width 47\n",
      "运行结果: start_depth 250, start_width 50, qr_map_depth 421, qr_map_width 46, qr_move_depth 298, qr_move_width 48\n",
      "运行结果: start_depth 250, start_width 50, qr_map_depth 421, qr_map_width 46, qr_move_depth 301, qr_move_width 48\n",
      "运行结果: start_depth 250, start_width 50, qr_map_depth 421, qr_map_width 46, qr_move_depth 358, qr_move_width 47\n",
      "运行结果: start_depth 250, start_width 50, qr_map_depth 421, qr_map_width 46, qr_move_depth 337, qr_move_width 48\n",
      "alg: mul, qubit_num: 10\n",
      "运行结果: start_depth 45, start_width 10, qr_map_depth 101, qr_map_width 3, qr_move_depth 65, qr_move_width 5\n",
      "运行结果: start_depth 45, start_width 10, qr_map_depth 101, qr_map_width 3, qr_move_depth 69, qr_move_width 5\n",
      "运行结果: start_depth 45, start_width 10, qr_map_depth 101, qr_map_width 3, qr_move_depth 69, qr_move_width 5\n",
      "运行结果: start_depth 45, start_width 10, qr_map_depth 101, qr_map_width 3, qr_move_depth 69, qr_move_width 5\n",
      "运行结果: start_depth 45, start_width 10, qr_map_depth 101, qr_map_width 3, qr_move_depth 69, qr_move_width 5\n",
      "alg: mul, qubit_num: 20\n",
      "运行结果: start_depth 85, start_width 20, qr_map_depth 221, qr_map_width 3, qr_move_depth 165, qr_move_width 5\n",
      "运行结果: start_depth 85, start_width 20, qr_map_depth 221, qr_map_width 3, qr_move_depth 173, qr_move_width 5\n",
      "运行结果: start_depth 85, start_width 20, qr_map_depth 221, qr_map_width 3, qr_move_depth 193, qr_move_width 4\n",
      "运行结果: start_depth 85, start_width 20, qr_map_depth 221, qr_map_width 3, qr_move_depth 165, qr_move_width 5\n",
      "运行结果: start_depth 85, start_width 20, qr_map_depth 221, qr_map_width 3, qr_move_depth 173, qr_move_width 5\n",
      "alg: mul, qubit_num: 30\n",
      "运行结果: start_depth 125, start_width 30, qr_map_depth 341, qr_map_width 3, qr_move_depth 285, qr_move_width 5\n",
      "运行结果: start_depth 125, start_width 30, qr_map_depth 341, qr_map_width 3, qr_move_depth 277, qr_move_width 5\n",
      "运行结果: start_depth 125, start_width 30, qr_map_depth 341, qr_map_width 3, qr_move_depth 285, qr_move_width 5\n",
      "运行结果: start_depth 125, start_width 30, qr_map_depth 341, qr_map_width 3, qr_move_depth 285, qr_move_width 5\n",
      "运行结果: start_depth 125, start_width 30, qr_map_depth 341, qr_map_width 3, qr_move_depth 281, qr_move_width 5\n",
      "alg: mul, qubit_num: 40\n",
      "运行结果: start_depth 165, start_width 40, qr_map_depth 461, qr_map_width 3, qr_move_depth 397, qr_move_width 5\n",
      "运行结果: start_depth 165, start_width 40, qr_map_depth 461, qr_map_width 3, qr_move_depth 397, qr_move_width 5\n",
      "运行结果: start_depth 165, start_width 40, qr_map_depth 461, qr_map_width 3, qr_move_depth 405, qr_move_width 5\n",
      "运行结果: start_depth 165, start_width 40, qr_map_depth 461, qr_map_width 3, qr_move_depth 397, qr_move_width 5\n",
      "运行结果: start_depth 165, start_width 40, qr_map_depth 461, qr_map_width 3, qr_move_depth 397, qr_move_width 5\n",
      "alg: mul, qubit_num: 50\n",
      "运行结果: start_depth 205, start_width 50, qr_map_depth 581, qr_map_width 3, qr_move_depth 525, qr_move_width 5\n",
      "运行结果: start_depth 205, start_width 50, qr_map_depth 581, qr_map_width 3, qr_move_depth 525, qr_move_width 5\n",
      "运行结果: start_depth 205, start_width 50, qr_map_depth 581, qr_map_width 3, qr_move_depth 525, qr_move_width 5\n",
      "运行结果: start_depth 205, start_width 50, qr_map_depth 581, qr_map_width 3, qr_move_depth 525, qr_move_width 5\n",
      "运行结果: start_depth 205, start_width 50, qr_map_depth 581, qr_map_width 3, qr_move_depth 525, qr_move_width 5\n",
      "alg: rd, qubit_num: 10\n",
      "运行结果: start_depth 16, start_width 10, qr_map_depth 49, qr_map_width 2, qr_move_depth 40, qr_move_width 3\n",
      "运行结果: start_depth 16, start_width 10, qr_map_depth 49, qr_map_width 2, qr_move_depth 40, qr_move_width 3\n",
      "运行结果: start_depth 16, start_width 10, qr_map_depth 49, qr_map_width 2, qr_move_depth 40, qr_move_width 3\n",
      "运行结果: start_depth 16, start_width 10, qr_map_depth 49, qr_map_width 2, qr_move_depth 37, qr_move_width 4\n",
      "运行结果: start_depth 16, start_width 10, qr_map_depth 49, qr_map_width 2, qr_move_depth 40, qr_move_width 3\n",
      "alg: rd, qubit_num: 20\n",
      "运行结果: start_depth 16, start_width 20, qr_map_depth 100, qr_map_width 3, qr_move_depth 67, qr_move_width 5\n",
      "运行结果: start_depth 16, start_width 20, qr_map_depth 100, qr_map_width 3, qr_move_depth 79, qr_move_width 4\n",
      "运行结果: start_depth 16, start_width 20, qr_map_depth 100, qr_map_width 3, qr_move_depth 46, qr_move_width 5\n",
      "运行结果: start_depth 16, start_width 20, qr_map_depth 100, qr_map_width 3, qr_move_depth 46, qr_move_width 5\n",
      "运行结果: start_depth 16, start_width 20, qr_map_depth 100, qr_map_width 3, qr_move_depth 79, qr_move_width 4\n",
      "alg: rd, qubit_num: 30\n",
      "运行结果: start_depth 16, start_width 30, qr_map_depth 169, qr_map_width 3, qr_move_depth 100, qr_move_width 5\n",
      "运行结果: start_depth 16, start_width 30, qr_map_depth 169, qr_map_width 3, qr_move_depth 118, qr_move_width 5\n",
      "运行结果: start_depth 16, start_width 30, qr_map_depth 169, qr_map_width 3, qr_move_depth 103, qr_move_width 5\n",
      "运行结果: start_depth 16, start_width 30, qr_map_depth 169, qr_map_width 3, qr_move_depth 103, qr_move_width 5\n",
      "运行结果: start_depth 16, start_width 30, qr_map_depth 169, qr_map_width 3, qr_move_depth 115, qr_move_width 5\n",
      "alg: rd, qubit_num: 40\n",
      "运行结果: start_depth 16, start_width 40, qr_map_depth 202, qr_map_width 3, qr_move_depth 169, qr_move_width 4\n",
      "运行结果: start_depth 16, start_width 40, qr_map_depth 202, qr_map_width 3, qr_move_depth 121, qr_move_width 5\n",
      "运行结果: start_depth 16, start_width 40, qr_map_depth 202, qr_map_width 3, qr_move_depth 133, qr_move_width 5\n",
      "运行结果: start_depth 16, start_width 40, qr_map_depth 202, qr_map_width 3, qr_move_depth 139, qr_move_width 5\n",
      "运行结果: start_depth 16, start_width 40, qr_map_depth 202, qr_map_width 3, qr_move_depth 169, qr_move_width 4\n",
      "alg: rd, qubit_num: 50\n",
      "运行结果: start_depth 19, start_width 50, qr_map_depth 253, qr_map_width 4, qr_move_depth 187, qr_move_width 4\n",
      "运行结果: start_depth 19, start_width 50, qr_map_depth 253, qr_map_width 4, qr_move_depth 190, qr_move_width 5\n",
      "运行结果: start_depth 19, start_width 50, qr_map_depth 253, qr_map_width 4, qr_move_depth 175, qr_move_width 5\n",
      "运行结果: start_depth 19, start_width 50, qr_map_depth 253, qr_map_width 4, qr_move_depth 184, qr_move_width 5\n",
      "运行结果: start_depth 19, start_width 50, qr_map_depth 253, qr_map_width 4, qr_move_depth 196, qr_move_width 4\n"
     ]
    }
   ],
   "source": [
    "# Default Settings\n",
    "# quantum_alg: bv\n",
    "# MRP_CP_ratio: 10\n",
    "# extra_qubit_ratio: 15%\n",
    "# qubit_num: 30\n",
    "# bv qaoa mod mul qram rd sym xor\n",
    "# for alg in [\"xor\"]:\n",
    "for alg in [\"bv\", \"qaoa\", \"mul\", \"rd\"]:\n",
    "    for qubit_num in [10, 20, 30, 40, 50]:\n",
    "        extra_num = 2\n",
    "        mrp_ratio = 10\n",
    "        print(f\"alg: {alg}, qubit_num: {qubit_num}\")\n",
    "        for _ in range(5):\n",
    "            (\n",
    "                start_depth,\n",
    "                start_width,\n",
    "                qr_map_depth,\n",
    "                qr_map_width,\n",
    "                qr_move_depth,\n",
    "                qr_move_width,\n",
    "            ) = experiment(alg, qubit_num, extra_num, mrp_ratio)\n",
    "            print(\n",
    "                f\"运行结果: start_depth {start_depth}, start_width {start_width}, qr_map_depth {qr_map_depth}, qr_map_width {qr_map_width}, qr_move_depth {qr_move_depth}, qr_move_width {qr_move_width}\"\n",
    "            )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bb06c64",
   "metadata": {},
   "outputs": [],
   "source": [
    "def experiment_var_qubit_num():\n",
    "    from concurrent.futures import ThreadPoolExecutor, as_completed\n",
    "    from func_timeout import func_timeout, FunctionTimedOut\n",
    "\n",
    "    # 存储每个参数组合的任务\n",
    "    all_tasks = []\n",
    "\n",
    "    # 创建所有参数组合\n",
    "    qubit_nums = [10, 20, 30, 40, 50]\n",
    "    algorithms = [\"bv\", \"vqa\", \"qaoa\", \"qft\", \"mul\", \"rd\", \"xor\"]\n",
    "\n",
    "    for qubit_num in qubit_nums:\n",
    "        for alg in algorithms:\n",
    "            MRP_CP_ratio = 10\n",
    "            extra_qubit_ratio = 0.15\n",
    "            all_tasks.append((alg, qubit_num, MRP_CP_ratio, extra_qubit_ratio))\n",
    "\n",
    "    # 为每个参数组合执行50次实验\n",
    "    def run_experiments_for_params(alg, qubit_num, MRP_CP_ratio, extra_qubit_ratio):\n",
    "        results = []\n",
    "        for _ in range(50):\n",
    "            try:\n",
    "                # 使用 func_timeout 设置超时时间为10分钟(600秒)\n",
    "                (\n",
    "                    start_depth,\n",
    "                    start_width,\n",
    "                    qr_map_depth,\n",
    "                    qr_map_width,\n",
    "                    qr_move_depth,\n",
    "                    qr_move_width,\n",
    "                ) = func_timeout(\n",
    "                    60 * 30,  # 超时时间（秒）\n",
    "                    experiment,\n",
    "                    args=(alg, qubit_num, extra_qubit_ratio * qubit_num, MRP_CP_ratio),\n",
    "                )\n",
    "\n",
    "                # 如果实验成功（不是全零结果），则添加到结果列表中\n",
    "                if not (\n",
    "                    start_depth == 0\n",
    "                    and start_width == 0\n",
    "                    and qr_map_depth == 0\n",
    "                    and qr_map_width == 0\n",
    "                    and qr_move_depth == 0\n",
    "                    and qr_move_width == 0\n",
    "                ):\n",
    "                    results.append(\n",
    "                        (\n",
    "                            start_depth,\n",
    "                            start_width,\n",
    "                            qr_map_depth,\n",
    "                            qr_map_width,\n",
    "                            qr_move_depth,\n",
    "                            qr_move_width,\n",
    "                        )\n",
    "                    )\n",
    "            except FunctionTimedOut:\n",
    "                print(f\"Experiment timed out for {alg} with {qubit_num} qubits\")\n",
    "            except Exception as e:\n",
    "                print(f\"Experiment failed with error: {e}\")\n",
    "                # 继续执行下一次实验\n",
    "\n",
    "        return alg, qubit_num, MRP_CP_ratio, extra_qubit_ratio, results\n",
    "\n",
    "    # 使用线程池并发执行所有实验\n",
    "    results_dict = {}\n",
    "    with ThreadPoolExecutor(max_workers=96) as executor:\n",
    "        # 提交所有任务\n",
    "        future_to_params = {\n",
    "            executor.submit(\n",
    "                run_experiments_for_params,\n",
    "                alg,\n",
    "                qubit_num,\n",
    "                MRP_CP_ratio,\n",
    "                extra_qubit_ratio,\n",
    "            ): (alg, qubit_num, MRP_CP_ratio, extra_qubit_ratio)\n",
    "            for alg, qubit_num, MRP_CP_ratio, extra_qubit_ratio in all_tasks\n",
    "        }\n",
    "\n",
    "        # 收集结果\n",
    "        for future in as_completed(future_to_params):\n",
    "            alg, qubit_num, MRP_CP_ratio, extra_qubit_ratio, valid_results = (\n",
    "                future.result()\n",
    "            )\n",
    "            results_dict[(alg, qubit_num)] = valid_results\n",
    "\n",
    "    # 处理和打印结果，并写入文件\n",
    "    # 准备写入CSV文件\n",
    "    with open(\"experiment_results.csv\", \"w\", newline=\"\") as csvfile:\n",
    "        fieldnames = [\n",
    "            \"algorithm\",\n",
    "            \"qubit_num\",\n",
    "            \"extra_qubit_ratio\",\n",
    "            \"MRP_CP_ratio\",\n",
    "            \"avg_start_depth\",\n",
    "            \"avg_start_width\",\n",
    "            \"avg_qr_map_depth\",\n",
    "            \"avg_qr_map_width\",\n",
    "            \"avg_qr_move_depth\",\n",
    "            \"avg_qr_move_width\",\n",
    "            \"valid_experiment_count\",\n",
    "        ]\n",
    "        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n",
    "        writer.writeheader()\n",
    "\n",
    "        for qubit_num in qubit_nums:\n",
    "            for alg in algorithms:\n",
    "                MRP_CP_ratio = 10\n",
    "                extra_qubit_ratio = 0.15\n",
    "                valid_results = results_dict.get((alg, qubit_num), [])\n",
    "\n",
    "                print(\n",
    "                    f\"-- 参数: {alg} 算法, 额外比特比例 {extra_qubit_ratio}, 量子比特数量 {qubit_num} --\"\n",
    "                )\n",
    "\n",
    "                # 计算平均值\n",
    "                if valid_results:\n",
    "                    avg_start_depth = sum(r[0] for r in valid_results) / len(\n",
    "                        valid_results\n",
    "                    )\n",
    "                    avg_start_width = sum(r[1] for r in valid_results) / len(\n",
    "                        valid_results\n",
    "                    )\n",
    "                    avg_qr_map_depth = sum(r[2] for r in valid_results) / len(\n",
    "                        valid_results\n",
    "                    )\n",
    "                    avg_qr_map_width = sum(r[3] for r in valid_results) / len(\n",
    "                        valid_results\n",
    "                    )\n",
    "                    avg_qr_move_depth = sum(r[4] for r in valid_results) / len(\n",
    "                        valid_results\n",
    "                    )\n",
    "                    avg_qr_move_width = sum(r[5] for r in valid_results) / len(\n",
    "                        valid_results\n",
    "                    )\n",
    "\n",
    "                    print(\n",
    "                        f\"平均值: {avg_start_depth}, {avg_start_width}, {avg_qr_map_depth}, {avg_qr_map_width}, {avg_qr_move_depth}, {avg_qr_move_width}\"\n",
    "                    )\n",
    "                    print(f\"有效实验次数: {len(valid_results)}\")\n",
    "\n",
    "                    # 写入CSV文件\n",
    "                    writer.writerow(\n",
    "                        {\n",
    "                            \"algorithm\": alg,\n",
    "                            \"qubit_num\": qubit_num,\n",
    "                            \"extra_qubit_ratio\": extra_qubit_ratio,\n",
    "                            \"MRP_CP_ratio\": MRP_CP_ratio,\n",
    "                            \"avg_start_depth\": avg_start_depth,\n",
    "                            \"avg_start_width\": avg_start_width,\n",
    "                            \"avg_qr_map_depth\": avg_qr_map_depth,\n",
    "                            \"avg_qr_map_width\": avg_qr_map_width,\n",
    "                            \"avg_qr_move_depth\": avg_qr_move_depth,\n",
    "                            \"avg_qr_move_width\": avg_qr_move_width,\n",
    "                            \"valid_experiment_count\": len(valid_results),\n",
    "                        }\n",
    "                    )\n",
    "                else:\n",
    "                    print(\"所有实验都失败了，无法计算平均值\")\n",
    "                    # 写入CSV文件，平均值为0\n",
    "                    writer.writerow(\n",
    "                        {\n",
    "                            \"algorithm\": alg,\n",
    "                            \"qubit_num\": qubit_num,\n",
    "                            \"extra_qubit_ratio\": extra_qubit_ratio,\n",
    "                            \"MRP_CP_ratio\": MRP_CP_ratio,\n",
    "                            \"avg_start_depth\": 0,\n",
    "                            \"avg_start_width\": 0,\n",
    "                            \"avg_qr_map_depth\": 0,\n",
    "                            \"avg_qr_map_width\": 0,\n",
    "                            \"avg_qr_move_depth\": 0,\n",
    "                            \"avg_qr_move_width\": 0,\n",
    "                            \"valid_experiment_count\": 0,\n",
    "                        }\n",
    "                    )\n",
    "\n",
    "\n",
    "# 调用实验函数\n",
    "experiment_var_qubit_num()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
